package com.lagou.rpc.consumer.controller;

import com.lagou.curator.ServerRegistryCenter;
import com.lagou.rpc.api.IUserService;
import com.lagou.rpc.consumer.ClientBootstrapApplication;
import com.lagou.rpc.consumer.proxy.RpcClientProxy;
import com.lagou.rpc.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @ClassName: UserController
 * @Description:
 * @Author: qjc
 * @Date: 2021/9/22 9:51 上午
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public User getUserById() {
        Map<Long, List<String>> mingTimeMap = new HashMap<>(2);
        for (String childrenPath : ClientBootstrapApplication.childrenList) {
            String childrenNodeData = ClientBootstrapApplication.serverRegistryCenter.getChildrenNodeData(childrenPath);
            String[] split = childrenNodeData.split(ServerRegistryCenter.RPC_COLON);
            if (split.length > 2) {
                long time = Long.valueOf(split[2]);
                if (mingTimeMap.containsKey(time)) {
                    List<String> list = mingTimeMap.get(time);
                    list.add(childrenPath);
                    mingTimeMap.put(time, list);
                } else {
                    List<String> list = new ArrayList<>();
                    list.add(childrenPath);
                    mingTimeMap.put(time, list);
                }
            }
        }
        if (mingTimeMap.size() > 0) {
            Long minKey = mingTimeMap.keySet().stream().min(Long::compareTo).get();
            List<String> minTimeChildrenList = mingTimeMap.get(minKey);
            for (String childrenPath : minTimeChildrenList) {
                String childrenNodeData = ClientBootstrapApplication.serverRegistryCenter.getChildrenNodeData(childrenPath);
                String[] split = childrenNodeData.split(ServerRegistryCenter.RPC_COLON);
                String ip = split[0];
                int port = Integer.parseInt(split[1]);
                String time = split[2];

                IUserService userService = (IUserService) RpcClientProxy.createProxy(IUserService.class, ip, port);
                User user = userService.getById(1, false);
                System.out.println("调用 " + childrenNodeData + " 结果：" + user);
                long l = System.currentTimeMillis();
                String value = ip + ServerRegistryCenter.RPC_COLON + port + ServerRegistryCenter.RPC_COLON + time + ServerRegistryCenter.RPC_COLON + l;
                ClientBootstrapApplication.serverRegistryCenter.updateChildrenNodeData(childrenPath, value);
                System.out.println("节点" + childrenPath + ",value:" + childrenNodeData + ",最新的value：" + value);
            }
        }
        return null;
    }
}
